package com.xxxx.controller;

import com.xxxx.entity.vo.MessageModel;
import com.xxxx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Autowired
    private UserService userService;
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接受客户端的请求（接受参数，姓名，密码）
        String uname=request.getParameter("uname");
        String upwd=request.getParameter("upwd");
        //2.调用service层的方法，返回消息模型对象
        MessageModel messageModel=userService.userLogin(uname,upwd);
        //3.判断消息模型的状态码
        if(messageModel.getCode(1)==1){
            //成功
            //用户信息放在session中，重定向index。jsp
            request.getSession().setAttribute("user",messageModel.getObject());
            response.sendRedirect("index");
        }else{ //失败
            //用户信息放在request作用域中，请求转发跳转到login.jsp中
            request.setAttribute("messageModel",messageModel);
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }
}
